<!--电子围栏页-->
<template>
  <div style="height:6vh;background-color: #fff;padding: 10px 20px">
    <span style="line-height: 60px;font-size:20px">围栏管理</span>
    <el-button type="primary" style="float: right; margin-top: 13px"
    @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>

  <el-card style="margin: 20px;height: 70px">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="geoSearchForm.name" @keydown.enter="loadGeo"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;">
        <el-select placeholder="请选择" style="width:220px;" v-model="geoSearchForm.status" @change="loadGeo">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadGeo">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

<!--  围栏表格数据  -->
  <el-card style="margin: 20px">
    <el-table stripe :data="geoTableData">
      <el-table-column label="编号" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" v-model="scope.row.status"
          @change="changeStatus(scope.row.id,scope.row.status)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作"  align="center">
        <template #default="scope">
          <el-button type="primary" link @click="loadVehicle(scope.row.id)">管理车辆</el-button>
          <el-button type="primary" link @click="deleteGeofence(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

<!--  已绑定车辆弹窗  -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible"
  style="padding: 40px;">
    <el-button type="primary" style="margin: 10px 0;" @click="showUnbindVehicleList">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column label="车辆品牌" prop="brand"  align="center"></el-table-column>
      <el-table-column label="车辆号"  prop="license" align="center"></el-table-column>
      <el-table-column label="车辆类型"  prop="type"  align="center"></el-table-column>
      <el-table-column label="操作"   fixed="right" width="200" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="removeBindVehicle(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
<!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible" :before-close="handleClose">
    <el-table :data="UnbindVehicleArr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column label="车辆品牌" prop="brand" align="center"></el-table-column>
      <el-table-column label="车牌号" prop="license" align="center"></el-table-column>
      <el-table-column label="车辆类型" prop="type" align="center"></el-table-column>
      <el-table-column label="车辆状态" prop="status" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary"  @click="bindNewVehicle(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script setup>
//控制"已绑定⻋辆弹窗"是否显⽰
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";
import router from "@/router";

const bindVehicleDialogVisible = ref(false);
//控制"可绑定⻋辆列表"是否显⽰
const AddBindVehicleDialogVisible = ref(false);

// 定义对象用来保存查询条件
const geoSearchForm = ref({name:'',status:''});

//定义数组用来保存围栏表格数据
const geoTableData = ref([]);
// 定义加载电子围栏的方法
const loadGeo=()=>{
  let data = qs.stringify(geoSearchForm.value)
  console.log(data);
  axios.get(BASE_URL+'/v1/geofence/select?'+data)
      .then((response)=>{
        if (response.data.code===2000){
          geoTableData.value=response.data.data;
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
onMounted(()=>{
  geoSearchForm.value={}
  loadGeo();
})
// 重置按钮的方法
const resetSearch=()=>{
  loadGeo();
}

// 定义全局变量用来保存当前电子围栏的id  这个变量是给后面解绑车辆时使用，数据更新显示
let geoId;
//定义数组用来保存当前围栏已绑定车辆数据
const bindVehicleArr = ref([{}]);
// 显示已绑定车辆数据
const loadVehicle=(geofenceId)=>{
  bindVehicleDialogVisible.value=true;
  // 给全局变量赋值
  geoId = geofenceId;
  axios.get(BASE_URL+'/v1/vehicle/select?geofenceId='+geofenceId)
      .then((response)=>{
        if(response.data.code===2000){
          bindVehicleArr.value=response.data.data;
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

// 移除围栏已绑定的车辆，注意这里的id是车辆的id
const removeBindVehicle=(id)=>{
  axios.post(BASE_URL+'/v1/vehicle/unbind/'+id)
      .then((response)=>{
        if(response.data.code===2000){
          ElMessage.success('车辆解绑成功')
          loadVehicle(geoId);
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
// 未绑定车辆列表数组
const UnbindVehicleArr = ref([])
const showUnbindVehicleList=()=>{
  AddBindVehicleDialogVisible.value=true;
  axios.get(BASE_URL+'/v1/vehicle/select/?geofenceBindStatus=0')
      .then((response)=>{
        if(response.data.code===2000){
          UnbindVehicleArr.value=response.data.data;
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

// 定义一个绑定车辆的方式，此处的id是车辆id
const bindNewVehicle = (id)=>{
  axios.post(BASE_URL+'/v1/vehicle/bind/'+geoId+'/'+id)
      .then((response)=>{
        if(response.data.code===2000){
          // AddBindVehicleDialogVisible.value=false;
          showUnbindVehicleList();
          ElMessage.success('车辆绑定成功！');
          // loadVehicle(geoId);
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
 // 处理内层弹窗关闭的方法
const handleClose=()=>{
  loadVehicle(geoId);
  AddBindVehicleDialogVisible.value=false;
}

// 定义修改围栏状态方法
const changeStatus =(id,status)=>{
  axios.post(BASE_URL+'/v1/geofence/update/'+id+"/"+status)
      .then((response)=>{
        if(response.data.code===2000){
          ElMessage.success('状态修改成功！');
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

const deleteGeofence =(id)=>{
  if(confirm('确认删除该围栏吗?')) {
    axios.post(BASE_URL + '/v1/geofence/delete/'+id)
        .then((response) => {
          if (response.data.code === 2000) {
            ElMessage.success('删除成功');
            loadGeo();
          } else {
            ElMessage.error('改围栏还有绑定车辆！');
          }
        })
  }
}
</script>

<style scoped>

</style>